namespace ContosoUniversity.Migrations
{
   using System;
   using System.Data.Entity.Migrations;

   public partial class Chap4 : DbMigration
   {
      public override void Up()
      {
         CreateTable(
             "dbo.Department",
             c => new
             {
                DepartmentID = c.Int(nullable: false, identity: true),
                Name = c.String(maxLength: 50),
                Budget = c.Decimal(nullable: false, storeType: "money"),
                StartDate = c.DateTime(nullable: false),
                InstructorID = c.Int(),
             })
             .PrimaryKey(t => t.DepartmentID)
             .ForeignKey("dbo.Instructor", t => t.InstructorID)
             .Index(t => t.InstructorID);

         CreateTable(
             "dbo.Instructor",
             c => new
             {
                InstructorID = c.Int(nullable: false, identity: true),
                LastName = c.String(nullable: false, maxLength: 50),
                FirstName = c.String(nullable: false, maxLength: 50),
                HireDate = c.DateTime(nullable: false),
             })
             .PrimaryKey(t => t.InstructorID);

         CreateTable(
             "dbo.OfficeAssignment",
             c => new
             {
                InstructorID = c.Int(nullable: false),
                Location = c.String(maxLength: 50),
             })
             .PrimaryKey(t => t.InstructorID)
             .ForeignKey("dbo.Instructor", t => t.InstructorID)
             .Index(t => t.InstructorID);

         CreateTable(
             "dbo.CourseInstructor",
             c => new
             {
                CourseID = c.Int(nullable: false),
                InstructorID = c.Int(nullable: false),
             })
             .PrimaryKey(t => new { t.CourseID, t.InstructorID })
             .ForeignKey("dbo.Course", t => t.CourseID, cascadeDelete: true)
             .ForeignKey("dbo.Instructor", t => t.InstructorID, cascadeDelete: true)
             .Index(t => t.CourseID)
             .Index(t => t.InstructorID);

         // Create  a department for course to point to.
         Sql("INSERT INTO dbo.Department (Name, Budget, StartDate) VALUES ('Temp', 0.00, GETDATE())");
         //  default value for FK points to department created aboves.
         AddColumn("dbo.Course", "DepartmentID", c => c.Int(nullable: false, defaultValue: 1));
         //AddColumn("dbo.Course", "DepartmentID", c => c.Int(nullable: false));

         AlterColumn("dbo.Course", "Title", c => c.String(maxLength: 50));
         AddForeignKey("dbo.Course", "DepartmentID", "dbo.Department", "DepartmentID", cascadeDelete: true);
         CreateIndex("dbo.Course", "DepartmentID");
      }

      public override void Down()
      {
         DropIndex("dbo.CourseInstructor", new[] { "InstructorID" });
         DropIndex("dbo.CourseInstructor", new[] { "CourseID" });
         DropIndex("dbo.OfficeAssignment", new[] { "InstructorID" });
         DropIndex("dbo.Department", new[] { "InstructorID" });
         DropIndex("dbo.Course", new[] { "DepartmentID" });
         DropForeignKey("dbo.CourseInstructor", "InstructorID", "dbo.Instructor");
         DropForeignKey("dbo.CourseInstructor", "CourseID", "dbo.Course");
         DropForeignKey("dbo.OfficeAssignment", "InstructorID", "dbo.Instructor");
         DropForeignKey("dbo.Department", "InstructorID", "dbo.Instructor");
         DropForeignKey("dbo.Course", "DepartmentID", "dbo.Department");
         AlterColumn("dbo.Course", "Title", c => c.String());
         DropColumn("dbo.Course", "DepartmentID");
         DropTable("dbo.CourseInstructor");
         DropTable("dbo.OfficeAssignment");
         DropTable("dbo.Instructor");
         DropTable("dbo.Department");
      }
   }
}
